var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onClick: chooseClass
    }
};
var settingCourseTree = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};


var ztree;
var zCoursetree;
function chooseClass(event, treeId, treeNode) {

    vm.schoolName=treeNode.name;
    vm.schoolId=treeNode.id;
    vm.planScore.schoolId = treeNode.id;
    vm.getcourseName();
    $("#courseSelect").show();
    vm.reload()

}

$(function () {
    vm.getCourseTree();

    $("#jqGrid").jqGrid({
        url: baseURL + "course/planscore/planScoreList",
        datatype: "json",
        colModel: [

            { label: '课程名称', name: 'courseName', index: 'course_id', width: 80 },
            { label: '学生姓名', name: 'name', index: 'name', width: 80 },
            { label: '课程成绩', name: 'courseScore', index: 'course_score', width: 80,
                formatter: function (data,index,row) {
                var re;

                    if(data==null)
                        re=`<div id="score${index.rowId}" ><label onclick="updateScore(${index.rowId},${row.courseId},${row.studentId})">未录入</label></div>`
                    else
                        re= `<div id="score${index.rowId}"><label onclick="updateScore(${index.rowId},${row.courseId},${row.studentId})">${data}</label></div>`;
                    return re;

                }

            },
            // { label: '修改', name: 'id', width: 80 ,
            //     formatter: function (data) {
            //
            //         return '<span class="label label-warning" onclick="update('+data+')">修改</span>';
            //
            //     }},
            // { label: '删除', name: 'id', width: 80 ,
            //     formatter: function (data) {
            //         return '<span class="label label-warning" onclick="del('+data+')">删除</span>';
            //
            //     }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        // multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
function getTest() {
    
}

function updateScore(id,courseId,studentId) {
    $(`#score${id}`).html(`<input id="input${id}" type="number" onchange="updateGo(${courseId},${studentId})" />`);
}
function updateGo(courseId,studentId) {
    var data={
        schoolClassId:vm.schoolId,
        courseScore:$(`#input${studentId}`).val(),
        studentId:studentId,
        courseId:courseId
    }
    console.log(data)
    $.ajax({
        type: "POST",
        url: baseURL + "course/planscore/updateScore",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function(r){
            console.log(r)
            vm.reload()
            //alert("g")
        }
    });

}

var vm = new Vue({
    el:'#rrapp',
    data:{
        selectId:null,
        schoolId:0,
        schoolName:"",
        showList: true,
        title: null,
        teachPlan: {},
        teachers:[],
        courseName:[],
        planScore: {
            name:null,
            schoolId:null,
            courseId:null
        },
        psList:[],
        menu:{
            parentName:null,
            parentId:0,
            type:1,
            orderNum:0
        }
    },
    methods: {
        getcourseNameList(){
            $.ajax({
                type: "POST",
                url: baseURL + "course/planscore/courseNameLis?id="+vm.schoolId,
                contentType: "application/json",
                data: JSON.stringify(vm.planScore),
                success: function(r){
                    console.log(r)
                    vm.directionList=r.page.list
                    console.log(r.page.list)
                }
            });

        },
        getPlanScoreList:function () {
            vm.reload();

        },
        courseTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = zCoursetree.getSelectedNodes();
                    if(node[0].id<10000) {
                        //选择上级菜单
                        vm.teachPlan.courseId=node[0].id;
                        vm.teachPlan.title = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        getCourseTree: function(menuId){
            //加载学生树
            $.get(baseURL + "/course/school/select", function(r){
                ztree = $.fn.zTree.init($("#schoolTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("id", 0);
                ztree.selectNode(node);
            })
            //加载课程树
            $.get(baseURL + "/course/menu/select", function(r){
                zCoursetree = $.fn.zTree.init($("#courseTree"), settingCourseTree, r.menuList);
                var node = ztree.getNodeByParam("id", 0);
                zCoursetree.selectNode(node);

                //    vm.menu.parentName = node.name;
            })
            //获取教师列表
            $.get(baseURL + "/course/teacher/getTeacherList", function(r){

                vm.teachers=r.teachers

                //    vm.menu.parentName = node.name;
            })



        },
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.planScore = {

            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.planScore.id == null ? "course/planscore/save" : "course/planscore/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.planScore),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "course/planscore/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "course/planscore/info/"+id, function(r){
                vm.planScore = r.planScore;
            });
        },
        getcourseName: function(){
            $.get(baseURL + 'course/planscore/getListBySchoolId?id='+vm.schoolId, function(r){
                 vm.courseName = r.page.list;


            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.planScore.courseId = vm.selectId;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                url:baseURL + "course/planscore/planScoreList",
                postData:
                    {
                        'courseId': vm.planScore.courseId,
                        'schoolId':vm.planScore.schoolId,
                        'name':vm.planScore.name
                    },
                page:page


            }).trigger("reloadGrid");

        }

    }

});

//选择多条记录
function getSelectedRows1() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }

    return grid.getGridParam("selarrrow");
}

function update(item) {
    var id = item;
    if(id == null){
        return ;
    }
    vm.showList = false;
    vm.title = "修改";
    vm.getInfo(id);
}

function del(item) {
    var ids = item;
    if(ids == null){
        return ;
    }

    confirm('确定要删除选中的记录？', function() {
        $.ajax({
            type: "GET",
            url: baseURL + "teachPlan/delete?id="+ids,
            success: function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                    $("#jqGrid").trigger("reloadGrid");

                } else {
                    alert(r.msg);
                }

            }
        });
    });
}